import '../../scss/uploadFault.scss'
import Vue from 'vue'
import MintUI from 'mint-ui'
import com from '../unit/common'
import UploadDom from '../unit/component/uploadImage'

Vue.use(MintUI)
import {Checklist, Indicator, MessageBox, Toast} from 'mint-ui';

Vue.component(Checklist.name, Checklist);
Vue.component('upload-dom', UploadDom);
window.onload = function () {
  let page = {
    _app: {},

    _eleCode: com.getUrlParam('eleCode') ? com.getUrlParam('eleCode') : '31103301052011010002',
    _init() {
      this._loadTemp()
    },
    _loadTemp() {
      let self = this
      this._app = new Vue({
        el: '#app',
        data() {
          return {
            isSubmit: false,
            uploadResult: true,
            options: [
              {
                label: '日常故障',
                value: '1',
              },
              {
                label: '一般故障',
                value: '2',
              },
              {
                label: '紧急故障',
                value: '3'
              },
            ],
            uploadMsg: {
              elCode: self._eleCode,
              userName: '',
              userPhone: '',
              description: '',
              type: '1',
            },
            uploadArr: [],
            errMsg: '',
            selectImage: '',
            imgMask: false,
          }
        },
        methods: {
          // 显示大图
          showBigImage(image) {
            this.selectImage = image
            this.imgMask = true
          },
          fileResult(data) {
            let fd = new FormData();
            // 压缩前,将图片转为二进制数据
            let blob = this.dataURItoBlob(data.base64);
            fd.append('file', blob, data.file.name);
            com.axio.post(`${com.basePath5}/oss-web/oss/upload`, fd)
              .then((response) => {
                this.uploadArr.push(`http://ceqs-test.oss-cn-shanghai.aliyuncs.com/${response.data.data}`)
              })
          },
          // base64 转换为二进制
          dataURItoBlob(dataURI) {
            let byteString = atob(dataURI.split(',')[1]);
            let mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
            let ab = new ArrayBuffer(byteString.length);
            let ia = new Uint8Array(ab);
            for (let i = 0; i < byteString.length; i++) {
              ia[i] = byteString.charCodeAt(i);
            }
            return new Blob([ab], {type: mimeString});
          },
          // 删除上传图片
          delUploadImg(index) {
            this.uploadArr.splice(index, 1)
          },
          checkSubmit() {
            const param = this.uploadMsg

            param.troubleImgUrl = this.uploadArr.join(',')
            console.log(param)
            if (param.userName === '') {
              MessageBox({
                message: '请输入您的姓名'
              })
              return false
            }
            if (!com.checkMobile(param.userPhone)) {
              this.errMsg = '请输入正确手机号'
              setTimeout(() => {
                this.errMsg = ''
              }, 1500)
              return false
            }
            if (param.description === '') {
              MessageBox({
                message: '请输入描述文字或图片'
              })
              return false
            }

            return true
          },
          // 提交错误结果
          uploadEleFault() {
            if (!this.checkSubmit()) {
              return
            }
            const param = this.uploadMsg
            param.troubleImgUrl = this.uploadArr.join(',')
            com.axio.post(`${com.basePath2}/tend/trouble/add`, param).then((res) => {
              this.isSubmit = true
              this.uploadResult = true
            }).catch((err) => {
              this.isSubmit = true
              this.uploadResult = false
            })
          },
          // 关闭页面
          closePage() {
            WeixinJSBridge.call('closeWindow');
          }
        }
      })
    }
  }
  page._init()
}